package org.libraq.org.service.impl;

import org.libraq.basic.service.impl.BaseServiceImpl;
import org.libraq.basic.util.PageList;
import org.libraq.org.domain.Department;
import org.libraq.org.mapper.DepartmentMapper;
import org.libraq.org.query.DepartmentQuery;
import org.libraq.org.service.IDepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class DepartmentServiceImpl
        extends BaseServiceImpl<Department>
        implements IDepartmentService {
    @Resource
    private DepartmentMapper departmentMapper;

    @Override
    public void save(Department department) {
        departmentMapper.save(department);
        buildDirPath(department);
    }

    @Override
    public void update(Department department) {
        buildDirPath(department);
    }

    public void buildDirPath(Department department){
        Department parent = department.getParent();
        String dirPath = "";
        if(parent.getId()==null){//如果没有父类， 就是一级部门， dirPath就是/id
            dirPath = "/"+department.getId();
        }else{//如果有父类，就抽取父类的dirPath 父类可能没有dirPath 只有id
            parent = departmentMapper.findById(parent.getId());
            dirPath = parent.getDirPath()+"/"+department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }

    @Override
    public List<Department> getDeptTree() {
        return departmentMapper.getDeptTree();
    }
}
